home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 August: Tool Chest / Apple_Developer_Group_August_1996_Tool_Chest.iso / Sample Code / Snippets / QuickDraw / Direct Pixel Access / Direct Pixel Access.c next >
Encoding:
C/C++ Source or Header  |  1992-10-14  |  6.4 KB  |  247 lines  |  [TEXT/KAHL]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    Direct Pixel Access                                        */
  4. /*                                                                            */
  5. /*    Description:    This snippet shows one example of how to directly        */
  6. /*                    change the pixel values stored in a pixel image.        */
  7. /*                    The original pixel image is obtained from a 'icl8'        */
  8. /*                    resource.  Only the first 20 columns of the first        */
  9. /*                    20 rows of the 'icl8' image is used.                    */
  10. /*                                                                            */
  11. /*    Files:            Direct Pixel Access.π                                    */
  12. /*                    Direct Pixel Access.π.rsrc                                */
  13. /*                    Direct Pixel Access.c                                    */
  14. /*                                                                            */
  15. /*    Programmer:        Edgar Lee                                                */
  16. /*    Organization:    Apple Computer, Inc.                                    */
  17. /*    Department:        Developer Technical Support, DTS                        */
  18. /*    Language:        C (Think C version 5.0.2)                                */
  19. /*    Date Created:    03-06-92                                                */
  20. /*                                                                            */
  21. /****************************************************************************/
  22.  
  23. /* Constant Declarations */
  24.  
  25. #define    WWIDTH    600
  26. #define    WHEIGHT    400
  27.  
  28. #define WLEFT    (((screenBits.bounds.right - screenBits.bounds.left) - WWIDTH) / 2)
  29. #define WTOP    (((screenBits.bounds.bottom - screenBits.bounds.top) - WHEIGHT) / 2)
  30.  
  31. /* Global Variable Definitions */
  32.  
  33. WindowPtr        gWindow;
  34. PixMapHandle    gPixmap;
  35.  
  36. void initMac();
  37.  
  38. void createWindow();
  39. void createOffscreen();
  40. void drawPixelImageData();
  41. void doEventLoop();
  42.  
  43. main()
  44. {
  45.     initMac();
  46.     
  47.     createWindow();
  48.     createOffscreen();
  49.     
  50.     doEventLoop();
  51. }
  52.  
  53. void initMac()
  54. {
  55.     MaxApplZone();
  56.  
  57.     InitGraf( &thePort );
  58.     InitFonts();
  59.     InitWindows();
  60.     InitMenus();
  61.     TEInit();
  62.     InitDialogs( nil );
  63.     InitCursor();
  64.     FlushEvents( 0, everyEvent );
  65. }
  66.  
  67. void createWindow()
  68. {
  69.     Rect        rect;
  70.  
  71.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  72.     gWindow = NewCWindow( 0L, &rect, "\pDirect Pixel Access", true, documentProc,
  73.                             (WindowPtr)-1L, true, 0L );                        
  74.     SetPort( gWindow );
  75.     
  76.     TextFont( geneva );
  77.     TextSize( 9 );
  78.     TextMode( srcXor );
  79. }
  80.  
  81. void createOffscreen()
  82. {
  83.     Rect        rect;
  84.     Handle        iclHandle;
  85.     
  86.     SetRect( &rect, 0, 0, 32, 32 );
  87.     
  88.     /* Create offscreen pixmap image using an 'icl8' icon resource. */
  89.  
  90.     iclHandle = GetResource( 'icl8', 129 );
  91.     HLock( iclHandle );
  92.     HNoPurge( iclHandle );
  93.     
  94.     gPixmap = (PixMapHandle)NewHandle( sizeof( PixMap ) );
  95.     
  96.     (**gPixmap).baseAddr = *iclHandle;
  97.     (**gPixmap).rowBytes = ((32 * 8) / 8) | 0x8000;
  98.     (**gPixmap).bounds = rect;
  99.     (**gPixmap).pmVersion = 0;
  100.     (**gPixmap).packType = 0;
  101.     (**gPixmap).packSize = 0;
  102.     (**gPixmap).hRes = 72;
  103.     (**gPixmap).vRes = 72;
  104.     (**gPixmap).pixelSize = 8;
  105.     (**gPixmap).planeBytes = 0;
  106.     (**gPixmap).pmReserved = 0;
  107.     (**gPixmap).pixelType = 0;
  108.     (**gPixmap).cmpCount = 1;
  109.     (**gPixmap).cmpSize = 8;
  110.     (**gPixmap).pmTable = GetCTable( 8 );
  111.     
  112.     /* Give a unique seed for the pixmap's colortable. */
  113.     (**(**gPixmap).pmTable).ctSeed = GetCTSeed();
  114. }
  115.  
  116. void drawPixelImageData()
  117. {
  118.     int                row, col;
  119.     Rect            rect;
  120.     unsigned char    value;
  121.     char            *image;
  122.     int                index = 0;
  123.     Str255            string;
  124.     RGBColor        color = { 32000, 32000, 32000 };
  125.     Byte            mode;
  126.     
  127.     ForeColor( blackColor );
  128.     
  129.     /* For this example, let's just use only the upper left corner of the image. */
  130.     SetRect( &rect, 0, 0, 20, 20 );
  131.     
  132.     /* Set the pointer to the beginning of the pixel image. */
  133.     image = GetPixBaseAddr( gPixmap );
  134.     
  135.     /*********************************************************************************/
  136.     /* Switch into 32-bit addressing mode before accessing the pixel image directly. */
  137.     /*********************************************************************************/
  138.  
  139.     mode = true32b;
  140.     SwapMMUMode( &mode );
  141.     
  142.     /*****************************************************************/
  143.     /* For this example, let's set the pixel values of the left half */
  144.     /*   of the image to half their original values.                 */
  145.     /*****************************************************************/
  146.     
  147.     for (row = 0; row < rect.bottom; row++)
  148.     {
  149.         /* Loop through the first 10 columns of the pixel image. */
  150.         for (index = 0, col = 0; col < rect.right / 2; col++)
  151.         {
  152.             /* Set the value at this index to half its value. */
  153.             value = (unsigned char)*(image + index);
  154.             *(image + index) = value / 2;
  155.             
  156.             index++;
  157.         }
  158.         
  159.         /* Increment the pointer to the next row of the pixel image. */
  160.         image += ((**gPixmap).rowBytes & 0x7fff);
  161.     }
  162.     
  163.     /********************************************************************/
  164.     /* Switch back to 24-bit addressing when done accessing the pixels. */
  165.     /********************************************************************/
  166.  
  167.     SwapMMUMode( &mode );
  168.     
  169.     /* Draw the offscreen image to the screen to see what it looks like. */
  170.     CopyBits( (BitMap *)*gPixmap, &gWindow->portBits, &rect,
  171.             &gWindow->portRect, srcCopy, 0 );
  172.     
  173.     RGBForeColor( &color );
  174.     
  175.     /* Again, set the pointer to the beginning of the pixel image. */
  176.     image = GetPixBaseAddr( gPixmap );
  177.     
  178.     /***************************************************************/
  179.     /* Finally let's display the pixel values on top of the image. */
  180.     /***************************************************************/
  181.     
  182.     /* Loop through the first 20 rows of the pixel image. */
  183.     for (row = 0; row < rect.bottom; row++)
  184.     {
  185.         /* Loop through the first 20 columns of the pixel image. */
  186.         for (index = 0, col = 0; col < rect.right; col++)
  187.         {
  188.             /* Get the value at this index into the pixel image. */
  189.             value = (unsigned char)*(image + index);
  190.             
  191.             MoveTo( col * 30, row * 20 );
  192.             LineTo( col * 30, (row + 1) * 20 );
  193.             LineTo( (col + 1) * 30, (row + 1) * 20 );
  194.             
  195.             MoveTo( (col * 30) + 6, (row * 20) + 14 );
  196.             NumToString( (long)value, &string );
  197.             DrawString( string );
  198.             
  199.             index++;
  200.         }
  201.         
  202.         /* Increment the pointer to the next row of the pixel image. */
  203.         image += ((**gPixmap).rowBytes & 0x7fff);
  204.     }
  205. }
  206.  
  207. void doEventLoop()
  208. {
  209.     EventRecord event;
  210.     WindowPtr   window;
  211.     short       clickArea;
  212.     Rect        screenRect;
  213.  
  214.     for (;;)
  215.     {
  216.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  217.         {
  218.             if (event.what == mouseDown)
  219.             {
  220.                 clickArea = FindWindow( event.where, &window );
  221.                 
  222.                 if (clickArea == inDrag)
  223.                 {
  224.                     screenRect = (**GetGrayRgn ()).rgnBBox;
  225.                     DragWindow( window, event.where, &screenRect );
  226.                 }
  227.                 else if (clickArea == inContent)
  228.                 {
  229.                     if (window != FrontWindow())
  230.                         SelectWindow( window );
  231.                 }
  232.                 else if (clickArea == inGoAway)
  233.                     if (TrackGoAway( window, event.where ))
  234.                         return;
  235.             }
  236.             else if (event.what == updateEvt)
  237.             {
  238.                 window = (WindowPtr)event.message;    
  239.                 SetPort( window );
  240.                 
  241.                 BeginUpdate( window );
  242.                 drawPixelImageData();
  243.                 EndUpdate( window );
  244.             }
  245.         }
  246.     }
  247. }